﻿using System;
using System.BLL;
using System.Collections.Generic;
using System.Dto;
using System.IBLL;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace System.MVC.Controllers
{
    /// <summary>
    /// 系统设置菜单控制器  内含系统设置、菜单管理、日志管理三个页面
    /// </summary>
    public class SystemController : Controller
    {
        /// <summary>
        /// 日志页面
        /// </summary>
        /// <returns></returns>
        public ActionResult LogList()
        {
            return View();
        }

        /// <summary>
        /// 日志页面的表格数据
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="LogTitle">日志内容</param>
        /// <param name="StaffName">操作人</param>
        /// <returns></returns>
        public JsonResult GetLogList(int limit, int page, string LogTitle, string StaffName)
        {
            ISystemManager SystemManagerScv = new SystemManager();
            int datalet;
            var data = SystemManagerScv.GetAllLogList(limit, page, LogTitle, StaffName, out datalet);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 菜单页面
        /// </summary>
        /// <returns></returns>
        public ActionResult MenusList()
        {
            return View();
        }
        /// <summary>
        /// 树形菜单页面
        /// </summary>
        /// <returns></returns>
        public ActionResult TreeMenusList()
        {
            return View();
        }
        /// <summary>
        /// 获取全部菜单数据
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllMenusList()
        {
            ISystemManager systemManagerScv = new SystemManager();
            var data = systemManagerScv.GetAllData();
            return Json(new { code = 0, msg = "获取成功", count = data.Count, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 菜单数据获取
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="title">菜单名</param>
        /// <param name="pid">父id</param>
        /// <returns></returns>
        public JsonResult GetMunlist(int limit, int page, string title, string pid)
        {
            ISystemManager systemManagerScv = new SystemManager();
            int datalet;
            var data = systemManagerScv.GetAllMenusList(limit, page, title, pid,out datalet);

            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加菜单的页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddMunes()
        {
            return View();
        }
        /// <summary>
        /// 添加菜单的页面添加请求接口
        /// </summary>
        /// <param name="dto">数据模型类</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddMunes(AddMenusDto dto)
        {
            ISystemManager systemManagerScv = new SystemManager();
            systemManagerScv.AddNewMnus(dto);
            systemManagerScv.AddLogs("菜单操作", $"新增了{dto.Title}菜单", Session["UserName"].ToString());
            return Json(new { code = 0, msg = "添加成功" });
        }

        /// <summary>
        /// 添加页面父IP的下拉选择表格数据的请求接口
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public JsonResult GettableSelectList(int limit, int page, string keyword)
        {
            ISystemManager systemManagerScv = new SystemManager();
            int datalet;
            var data = systemManagerScv.GettableSelectList(limit, page, keyword, out datalet);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除菜单按钮的数据请求接口
        /// </summary>
        /// <param name="id">菜单ID</param>
        /// <returns></returns>
        public JsonResult DelMunes(int id)
        {
            ISystemManager systemManagerScv = new SystemManager();
            systemManagerScv.DelMenusById(id);
            systemManagerScv.AddLogs("菜单操作", $"删除了ID为{id}的菜单", Session["UserName"].ToString());
            return Json(new { code = 0, msg = "删除成功" }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 菜单修改的页面
        /// </summary>
        /// <param name="id">菜单ID</param>
        /// <returns></returns>
        public ActionResult EditMunes(int id)
        {
            ISystemManager systemManagerScv = new SystemManager();
            var data = systemManagerScv.GetOneMenusById(id);
            return View(data);
        }
        /// <summary>
        /// 菜单修改页面保存的数据接口
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditMune(AllMenusListDto listDto)
        {
            ISystemManager systemManagerScv = new SystemManager();
            systemManagerScv.EditOneMenus(listDto);
            systemManagerScv.AddLogs("菜单操作", $"修改了{listDto.Title}菜单", Session["UserName"].ToString());
            return Json(new { code = 0, msg = "修改成功" });
        }
    }
}